<template>
  <div class="pages_content table">
    <el-container>
      <el-header>
        <breaDcrumb />
      </el-header>
      <el-main style="display: flex">
        <div>
          <div class="header_right" @click="showCardNav = !showCardNav">
            <div class="title">搜索</div>
            <div>
              <transition name="from"></transition>
              <span v-show="!showCardNav">
                <i style="margin-right: 5px" class="el-icon-arrow-down"></i>
              </span>
              <transition name="to"></transition>
              <span v-show="showCardNav">
                <i style="margin-right: 5px" class="el-icon-arrow-up"></i>
              </span>
            </div>
          </div>
          <div v-show="showCardNav" class="hr"></div>
          <el-collapse-transition>
            <div v-show="showCardNav">
              <div class="search">
                <span style="display: inline-block;margin-bottom: 10px;">
                  <span style="margin-right: 12px">名称</span>
                  <el-input v-model="tableParam.name" style="width: 240px" clearable placeholder="请输入" @keyup.enter.native="gettableData()" />
                </span>
                <span style="margin-left: 24px">
                  <span style="margin-right: 12px">规格</span>
                  <el-input v-model="tableParam.specificationName" style="width: 240px" clearable placeholder="请输入" @keyup.enter.native="gettableData()" />
                </span>
                <span style="margin-left: 24px">
                  <span style="margin-right: 12px">类型</span>
                  <el-input v-model="tableParam.type" style="width: 240px" clearable placeholder="请输入" @keyup.enter.native="gettableData()" />
                </span>
                <el-button icon="el-icon-search" size="medium" class="bottom_all" @click="(tableParam.page = 1), gettableData()">查 询</el-button>
                <el-button size="medium" class="bottom_all_two" style="margin-left: 12px" @click="init()">重 置</el-button>
              </div>
            </div>
          </el-collapse-transition>
          <div class="hr"></div>
          <div class="card">
            <div class="card_nav">
              <div>
                <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='TIANJIA'))!==-1" size="small" class="bottom_all" style="margin-left: 0" @click="pagesaddFromDate('添加')">
                  <i class="icon iconfont iconicon_button_add" style="font-size: 12px"></i>
                  添加报废周材
                </el-button>
              </div>
              <div>
                <el-dropdown :hide-on-click="false" trigger="click" style="margin: 0 20px">
                  <el-link :underline="false">
                    <span class="el-icon--left">
                      <i class="iconfont iconicon_button_filtrate"></i>
                    </span>
                    筛选
                  </el-link>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item v-for="(item, index) in table" :key="index">
                      <el-checkbox v-model="item.status">{{
                        item.label
                        }}</el-checkbox>
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </div>
            </div>
            <div v-loading="tableLoading" class="card_info" :style="style">
              <el-table :header-cell-style="{ background: '#f3f6f9' }" :cell-style="{ borderRight: 'none' }" border stripe style="width: 100%" :data="tableData" tooltip-effect="dark">
                <template v-for="(item, index2) in table">
                  <el-table-column v-if="item.status" :key="index2" :label="item.label" align="center" :min-width="item.width" :fixed="item.label =='操作'?'right':false">
                    <template slot-scope="scope">
                      <div v-if="item.label == '操作'">
                        <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='XIUGAI'))!==-1" class="bottom_one" size="mini" @click="click(scope.row)">
                          修改
                        </el-button>
                      </div>
                      <div v-else-if="item.label == '报废方式'">
                        {{scope.row.scrapType==1?'变卖':'调拨'}}
                      </div>
                      <span v-else>{{ scope.row[item.prop] }}</span>
                    </template>
                  </el-table-column>
                </template>
              </el-table>
            </div>
            <!-- <div class="card_bottom">
              <div class="card_bottom_left">
                <div class="top_page_totle">总共{{ total }}条</div>
                <el-pagination :current-page.sync="tableParam.page" :page-size="tableParam.pageSize" layout="prev, pager, next, jumper" :total="total" @current-change="gettableData()"></el-pagination>
              </div>
              <div>
                <el-button size="mini" icon="el-icon-refresh-right" class="bottom_all_two" @click="gettableData">刷新</el-button>
              </div>
            </div> -->
          </div>
        </div>
      </el-main>
      <div class="card_bottom">
        <div class="card_bottom_left">
          <div class="top_page_totle">总共{{ total }}条</div>
          <el-pagination :current-page.sync="tableParam.page" :page-size="tableParam.pageSize" layout="prev, pager, next, jumper" :total="total" @current-change="gettableData()"></el-pagination>
        </div>
        <div>
          <el-button size="mini" icon="el-icon-refresh-right" class="bottom_all_two" @click="gettableData">刷新</el-button>
        </div>
      </div>
    </el-container>
    <!-- 添加 -->
    <el-dialog v-dialogDrag :visible.sync="addDialog" width="540px" :close-on-click-modal="false" :before-close="resetFromDate">
      <span slot="title" class="dialog-title">{{ addDialogTitle }}</span>
      <div class="dialoginfo">
        <el-form ref="reffromDate" :rules="rules" :inline="true" :model="fromDate" label-width="110px">
          <el-form-item prop="name">
            <span slot="label" style="color: #ff0000">名称：</span>
            <el-select v-model="fromDate.name" placeholder="请选择" @change="change()">
              <el-option v-for="(item, index) in nameList" :key="index" :value="item"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="specificationId">
            <span slot="label" style="color: #ff0000">规格：</span>
            <el-select v-model="fromDate.specificationId" placeholder="请选择" :disabled="specificationList.length==0" @change="change1()">
              <el-option v-for="(item, index) in specificationList" :key="index" :label="item.specificationName" :value="item.specificationId"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="typeId">
            <span slot="label" style="color: #ff0000">类型：</span>
            <el-select v-model="fromDate.typeId" placeholder="请选择" :disabled="typeList.length==0" @change="change2()">
              <el-option v-for="(item, index) in typeList" :key="index" :label="item.type" :value="item.typeId"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="amount">
            <span slot="label" style="color: #ff0000">报废数量：</span>
            <el-input v-model="fromDate.amount" type="Number" placeholder="请输入" clearable>
              <template slot="append">根</template>
            </el-input>
          </el-form-item>
          <el-form-item prop="scrapTime">
            <span slot="label">报废时间：</span>
            <el-date-picker v-model="fromDate.scrapTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item prop="scrapType">
            <span slot="label">报废方式：</span>
            <el-radio-group v-model="fromDate.scrapType">
              <el-radio :label="1">变卖</el-radio>
              <el-radio :label="2">调拨</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item prop="receivingUnit">
            <span slot="label">报废单位</span>
            <el-input v-model="fromDate.receivingUnit" placeholder="请输入" clearable>
            </el-input>
          </el-form-item>
          <el-form-item prop="reason">
            <span slot="label">报废原因：</span>
            <el-input v-model="fromDate.reason" placeholder="请填写备注" type="textarea" resize="none" rows="5"></el-input>
          </el-form-item>
          <el-form-item prop="remark">
            <span slot="label">备注：</span>
            <el-input v-model="fromDate.remark" placeholder="请填写备注" type="textarea" resize="none" rows="5"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="bottom_all_two" @click="resetFromDate">取 消</el-button>
        <el-button class="bottom_all" style="margin-left: 24px" @click="pagessubmitFromDate()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改 -->
    <el-dialog v-dialogDrag :visible.sync="dialog" width="540px" :close-on-click-modal="false">
      <span slot="title" class="dialog-title">{{ addDialogTitle }}</span>
      <div class="dialoginfo">
        <el-form ref="reffromDate" :rules="rules" :inline="true" :model="low" label-width="110px">
          <el-form-item>
            <span slot="label" style="color: #ff0000">名称：</span>
            {{low.name}}
          </el-form-item>
          <div>
            <el-form-item>
              <span slot="label" style="color: #ff0000">规格：</span>
              {{low.specificationName}}
            </el-form-item>
          </div>
          <div>
            <el-form-item>
              <span slot="label" style="color: #ff0000">类型：</span>
              {{low.type}}
            </el-form-item>
          </div>
          <div>
            <el-form-item>
              <span slot="label" style="color: #ff0000">报废数量：</span>
              {{low.amount}}
            </el-form-item>
          </div>
          <el-form-item prop="scrapTime">
            <span slot="label">报废时间：</span>
            <el-date-picker v-model="low.scrapTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item prop="scrapType">
            <span slot="label">报废方式：</span>
            <el-radio-group v-model="low.scrapType">
              <el-radio :label="1">变卖</el-radio>
              <el-radio :label="2">调拨</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item prop="receivingUnit">
            <span slot="label">报废单位</span>
            <el-input v-model="low.receivingUnit" placeholder="请输入" clearable>
            </el-input>
          </el-form-item>
          <el-form-item prop="reason">
            <span slot="label">报废原因：</span>
            <el-input v-model="low.reason" placeholder="请填写备注" type="textarea" resize="none" rows="5"></el-input>
          </el-form-item>
          <el-form-item prop="remark">
            <span slot="label">备注：</span>
            <el-input v-model="low.remark" placeholder="请填写备注" type="textarea" resize="none" rows="5"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="bottom_all_two" @click="dialog=false">取 消</el-button>
        <el-button class="bottom_all" style="margin-left: 24px" @click="sunmit1()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import mixin from "@/mixins/ICPSmixin.js";
import {
  queryPage,
  saveOrUpdate,
  queryNameList,
  querySpecification,
  saveOneResord
} from "@/api/besSer/WMmanagement/scrap.js";
import { getUserInfo } from "@/utils/auth"
import { sortReg } from "@/common/RegExp.js";
export default {
  mixins: [mixin],
  data() {
    var validatePass1 = (rule, value, callback) => {
      if (sortReg(value)) {
        callback();
      } else {
        callback(new Error("请输正整数!"));
      }
    };
    return {
      /*表格增删改查及下载接口*/
      queryPage: queryPage,
      saveOrUpdate: saveOrUpdate,
      /*表格数据*/
      table: [
        { label: "名称", width: "120", prop: "name", status: true },
        { label: "规格", width: "120", prop: "specificationName", status: true },
        { label: "等级/类型", width: "120", prop: "type", status: true },
        { label: "报废数量(根)", width: "120", prop: "amount", status: true },
        { label: "报废日期", width: "160", prop: "scrapTime", status: true },
        { label: "报废方式", width: "120", prop: "", status: true },
        { label: "报废原因", width: "260", prop: "reason", status: true },
        { label: "操作", width: "120", prop: "", status: true },
      ],
      pagesType: 0,
      /*添加表单数据*/
      rules: {
        amount: [{ required: true, message: "请输入报废数量", trigger: "blur" },
        { validator: validatePass1, trigger: "blur", }],
        name: [{ required: true, message: "请选择名称", trigger: "change" }],
        specificationId: [{ required: true, message: "请选择规格", trigger: "change" }],
        typeId: [{ required: true, message: "请选择类型", trigger: "change" }],
        scrapTime: [{ required: true, message: "请选择报废时间", trigger: "change" }],
        receivingUnit: [{ required: true, message: "请输入报废单位", trigger: "change" }],
      },
      fromDate: {
        name: "",
        specificationId: '',
        typeId: '',
        scrapType: 1
      },
      nameList: [],
      typeList: [],
      specificationList: [],
      dialog: false,
      low: {}
    };
  },
  created() {
    /*存储拷贝的原初始化fromDate数据*/
    this.tableParam = { ...this.tableParam, name: '', specificationName: '', type: '' };
    this.gettableData();
  },
  methods: {
    /*重置数据,此方法必须存在，并且方发只重置条件*/
    init() {
      this.tableParam.name = "";
      this.tableParam.specificationName = "";
      this.tableParam.type = "";
      this.gettableData();
    },
    async pagesaddFromDate(text) {
      const { data } = await queryNameList()
      this.nameList = data
      this.typeList = []
      this.specificationList = []
      this.addFromDate(text)
    },
    async change() {
      const { data } = await querySpecification({ name: this.fromDate.name, companyId: getUserInfo().companyId })
      this.specificationList = data
      this.fromDate.specificationId = ''
      this.fromDate.typeId = ''
      this.typeList = []
    },
    change1() {
      let a = this.specificationList.find((res) => (res.specificationId == this.fromDate.specificationId))
      this.fromDate.typeId = ''
      this.fromDate.specificationName = a.specificationName
      this.typeList = a.typeList
    },
    change2() {
      let a = this.typeList.find((res) => (res.typeId == this.fromDate.typeId))
      this.fromDate.type = a.type
      this.fromDate.repertoryManageId = a.id
    },
    pagessubmitFromDate() {
      let _this = this;
      _this.$refs.reffromDate.validate((valid) => {
        if (valid) {
          _this.fromDate.companyId = getUserInfo().companyId
          _this.fromDate.company = getUserInfo().companyName
          _this.submitFromDate();
        } else {
          return false;
        }
      });
    },
    /**修改*/
    click(low) {
      this.low = this.copy(low)
      this.dialog = true
    },
    sunmit1() {
      saveOneResord(this.low).then(res => {
        if (res.code === 1001) {
          this.$message({
            message: '保存成功',
            type: "success",
          });
          this.dialog = false
          this.gettableData()
        }
      })
    },

  },
};
</script>

<style lang="scss" scoped>
.el-input,
.el-select,
.el-textarea {
  width: 320px;
}
/*
 <el-link class="table_link" :underline="false">查看配合比</el-link>*/
.table_link {
  margin: 0 10px;
  color: $bg_color;
  cursor: pointer;
}

.table_link:hover {
  opacity: 0.8;
}
</style>
